define(['jquery',"bbcare","bld/app/classify/programList.servers","bld/app/classify/programList.ui"],function($,$bb,$servers,$ui){
    var programList = {};

    /**
     * 删除方案回调函数
     */
    var ajaxDeleteScheme = function(data) {
        $bb.toastShow('删除成功');
        programList.init();
    }

    /**
     * 确认删除方案
     */
    var confirmDeleteScheme = function(params) {
        $servers.ajaxDeleteScheme(params,ajaxDeleteScheme);
    }

    /**
     * 删除方案
     */
    var onDeleteScheme = function() {
        var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
        var channelId = $bb.getChannelId();
        var id = $(this).parents(".program-item").attr('id');
        var schemeName = $(this).parent().prev().html();
        var params = {
            "oprInfo":{
                "opCode": "2068",
                "opType": "3",
                "opNote": "【" + doctorName + "】医生删除方案【" + schemeName + "】",
                "channelType":"2",
                "channelId":channelId
            },
            "schemeInfo":{
                "id":id,
            }
        };
        $bb.toastConfirmShow(confirmDeleteScheme,params,"确认是否删除？")
    }

    /**
     * 编辑方案
     */
    var onEditProgram = function(){
        var schemes = $(this).data("schemes");
        var index = $(this).parents(".program-item").index();
        var params = {
            "schemeId":schemes[index].id,
            "schemeName":schemes[index].scheme_name,
            "classId":schemes[index].class_id,
            "schemeVersion":schemes[index].new_version,
            "status":schemes[index].status,
            "diseaseId": schemes[index].disease_id,
            "libId": schemes[index].lib_id,
        };
        $bb.openPage('bld_programEdit',params);
    }

    /**
     * 复制方案回调函数
     */
    var ajaxCopyScheme = function(data) {
        $bb.toastShow('复制方案成功');
        programList.init();
    }

    /**
     * 复制方案
     */
    var onCopyScheme = function() {
        var schemes = $(this).data("schemes");
        var index = $(this).parents(".program-item").index();
        var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
        var channelId = $bb.getChannelId();
        var params = {
            "oprInfo":{
                "opCode": "2069",
                "opType": "3",
                "opNote": "【" + doctorName + "】医生复制方案【" + schemes[index].scheme_name + "】",
                "channelType": "2",
                "channelId": channelId
            },
            "schemeId":schemes[index].id,
            "schemeVersion":schemes[index].new_version,
        };
        $servers.ajaxCopyScheme(params,ajaxCopyScheme);
    }

    /**
     * 查询方案列表成功后的回调函数
     */
    var ajaxQueryAllSchemeCallBack = function(data){
        $ui.queryAllScheme({
            "id": "programList",
            "list": data.rows
        });
        /* 删除方案 */
        $(".delProgram").unbind('click');
        $(".delProgram").click(onDeleteScheme);
        /* 编辑方案 */
        $(".editProgram,.scheme-name").unbind('click');
        $(".editProgram,.scheme-name").data("schemes",data.rows);
        $(".editProgram,.scheme-name").click(onEditProgram);
        /* 复制方案 */
        $(".copyProgram").unbind('click');
        $(".copyProgram").data("schemes",data.rows);
        $(".copyProgram").click(onCopyScheme);
    }

    /**
     * 添加方案回调函数
     */
    var ajaxAddSchemeCallBack = function(data) {
        $('.programListModal').modal('hide');
        $bb.toastShow('添加方案成功');
        programList.init();
    }

    /**
     * 添加方案
     */
    var onAddScheme = function() {
        if( schemeName == '' ){
            $bb.toastShow('请填写方案名称');
        }else{
            var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
            var channelId = $bb.getChannelId();
            var schemeName = $("#schemeName").val();
            var obj = $bb.getPageParams();
            var params = {
                "oprInfo":{
                    "opCode": "2066",
                    "opType": "3",
                    "opNote": "【" + doctorName + "】医生添加方案【" + schemeName + "】",
                    "channelType": "2",
                    "channelId": channelId
                },
                "schemeInfo":{
                    "schemeName": schemeName,
                    "classId": obj.id,
                    "diseaseId": obj.disease_id,
                    "libId": obj.lib_id
                }
            };
            $servers.ajaxAddScheme(params,ajaxAddSchemeCallBack);
        }
    }

    /**
     * 添加方案
     */
    var onAddClick = function() {
        var className = "programListModal";
        $bb.modal({
            "className": className,
            "tit": "添加方案",
            "size":"modal-sm"
        }, function() {
            $ui.add({
                "dom": $("." + className + " .modal-body"),
            });
            $("." + className).modal("show");
            $("#add").unbind('click');
            $("#add").click(onAddScheme);
        });
    }

    programList.init = function() {
        var obj = $bb.getPageParams();
        var params = {
            "queryCriteria":{
                "classId": obj.id
            }
        };
        $servers.ajaxQueryAllScheme(params,ajaxQueryAllSchemeCallBack);
        // 添加方案
        $("#addBtn").unbind("click");
        $("#addBtn").click(onAddClick);
        // 返回方案分类页面
        $("#nav").unbind('click');
        $("#nav").click(function() {
            $bb.openPage('bld_classifyList');
        });
    }

    return programList;
});